package cs342.uwosh.edu.ContactsApp;

import cs342.uwosh.edu.ContactsApp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DisplayContact extends Activity {
	static long id;
	private ContactsDataSource db;
	private TextView fName, lName, cPhone, hPhone, aPhone, address, email, comment;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    	switch(ContactsActivity.theme)
    	{
    		case 0:
    			this.setTheme(R.style.myDark);
    			break;
    		case 1:
    			this.setTheme(R.style.myLight);
    			break;
    		case 2:
    			this.setTheme(R.style.myMatrix);
    			break;
    	}
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        
        db = new ContactsDataSource(this);
        
        fName = (TextView) findViewById(R.id.firstNameText);
    	lName = (TextView) findViewById(R.id.lastNameText);
    	cPhone = (TextView) findViewById(R.id.cellPhoneText);
    	hPhone = (TextView) findViewById(R.id.homePhoneText);
    	aPhone = (TextView) findViewById(R.id.altPhoneText);
    	address = (TextView) findViewById(R.id.addressText);
    	email = (TextView) findViewById(R.id.emailText);
    	comment = (TextView) findViewById(R.id.commentText);
    	
    	db = new ContactsDataSource(this);
    	db.open();
    	Contact contact = db.getContact(id);
    	db.close();
    	
    	String first="";
    	String last="";
    	int split = contact.getName().indexOf(' ');
    	if(split > 0) {
    		first = contact.getName().substring(0, split);
    		last = contact.getName().substring(split+1);
    	}else {
    		first = contact.getName();
    	}
    	
    	fName.setText(first);
    	lName.setText(last);
    	cPhone.setText(contact.getCellPhoneNumber());
    	hPhone.setText(contact.getHomePhoneNumber());
    	aPhone.setText(contact.getAlternatePhoneNumber());
    	address.setText(contact.getAddress());
    	email.setText(contact.getEmail());
    	comment.setText(contact.getComments());
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.edit_menu, menu);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    		case R.id.editOptionsMenu:
    			this.edit(getCurrentFocus());
    			break;
    		case R.id.deleteOptionsMenu:
    			this.delete(getCurrentFocus());
    			break;
    	}
    	return true;
    }
    
    public void edit(View view) {
    	EditContact.id = DisplayContact.id;
    	Intent i = new Intent(this, EditContact.class);
    	startActivity(i);
    }
    
    public void delete(View view) {
    	db.open();
    	Contact contact = db.getContact((long) id);
    	db.deleteContact(contact);
    	db.close();
    	
    	Intent i = new Intent(this, ContactsActivity.class);
    	startActivity(i);
    }
    
}